Skip to content

Method: {...}

1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * SteelBlue: DCI User Interfaces
5: * http://tidalwave.it/projects/steelblue
6: *
7: * Copyright (C) 2015 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *************************************************************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/steelblue-src
22: * git clone https://github.com/tidalwave-it/steelblue-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.ui.core.role.spi;
27:
28: import javax.annotation.Nonnull;
29: import java.util.Collection;
30: import java.util.List;
31: import it.tidalwave.util.As;
32: import it.tidalwave.util.RoleFactory;
33: import it.tidalwave.util.Task;
34: import it.tidalwave.util.spi.SimpleFinderSupport;
35: import it.tidalwave.role.SimpleComposite;
36: import it.tidalwave.role.impl.ContextSnapshot;
37: import it.tidalwave.ui.core.role.Presentable;
38: import it.tidalwave.ui.core.role.PresentationModel;
39: import it.tidalwave.ui.core.role.PresentationModelFactory;
40: import lombok.RequiredArgsConstructor;
41: import lombok.extern.slf4j.Slf4j;
42: import static java.util.Collections.emptyList;
43: import static java.util.stream.Collectors.*;
44: import static it.tidalwave.util.ShortNames.*;
45: import static it.tidalwave.role.SimpleComposite._SimpleComposite_;
46:
47: /***************************************************************************************************************************************************************
48: *
49: * An implementation of {@link Presentable} for datum instances having the {@link SimpleComposite} role.
50: *
51: * @stereotype Role
52: * @since 2.0-ALPHA-1
53: * @author Fabrizio Giudici
54: *
55: **************************************************************************************************************************************************************/
56: @Slf4j
57: public class SimpleCompositePresentable implements Presentable
58: {
59: private static final As.Type<SimpleComposite<As>> _SimpleCompositeOfAs_ = As.type(_SimpleComposite_);
60:
61: @RequiredArgsConstructor
62: static class SCPFinder extends SimpleFinderSupport<PresentationModel>
63: {
64: // private static final long serialVersionUID = -3235827383866946732L;
65:
66: @Nonnull
67: private final SimpleCompositePresentable scp;
68:
69: @Nonnull
70: private final Collection<Object> roles;
71:
72: public SCPFinder (@Nonnull final SCPFinder other, @Nonnull final Object override)
73: {
74: super(other, override);
75: final var source = getSource(SCPFinder.class, other, override);
76: this.scp = source.scp;
77: this.roles = source.roles;
78: }
79:
80: @Override @Nonnull
81: protected List<PresentationModel> computeResults()
82: {
83: return scp.contextSnapshot.runWithContexts(new Task<>()
84: {
85: @Override @Nonnull
86: public List<PresentationModel> run()
87: {
88: final List<As> children = scp.datum.maybeAs(_SimpleComposite_)
89: .map(c -> c.findChildren().results()).orElse(emptyList());
90: return children.stream()
91: .map(child -> child.maybeAs(_Presentable_)
92: .orElseGet(() -> new SimpleCompositePresentable(child)))
93: .map(presentable -> presentable.createPresentationModel(roles))
94: .collect(toList());
95: }
96: });
97: }
98: }
99:
100: private static final long serialVersionUID = 324646965695684L;
101:
102: @Nonnull
103: private final As datum;
104:
105: // This is not @Injected to avoid a dependency on Spring AOP
106: @Nonnull
107: private final PresentationModelFactory defaultPresentationModelFactory;
108:
109: private final ContextSnapshot contextSnapshot;
110:
111: /***********************************************************************************************************************************************************
112: * @param datum the owner
113: **********************************************************************************************************************************************************/
114: public SimpleCompositePresentable (@Nonnull final As datum)
115: {
116: this(datum, new DefaultPresentationModelFactory());
117: }
118:
119: /***********************************************************************************************************************************************************
120: * @param datum the owner
121: * @param defaultPresentationModelFactory the {@code PresentationModelFactory}
122: **********************************************************************************************************************************************************/
123: public SimpleCompositePresentable (@Nonnull final As datum,
124: @Nonnull final PresentationModelFactory defaultPresentationModelFactory)
125: {
126: this.datum = datum;
127: this.defaultPresentationModelFactory = defaultPresentationModelFactory;
128: contextSnapshot = new ContextSnapshot(datum);
129: }
130:
131: /***********************************************************************************************************************************************************
132: * {@inheritDoc}
133: **********************************************************************************************************************************************************/
134: @Override @Nonnull
135: public PresentationModel createPresentationModel (@Nonnull final Collection<Object> roles)
136: {
137: return internalCreatePresentationModel(datum, roles);
138: }
139:
140: /***********************************************************************************************************************************************************
141: *
142: **********************************************************************************************************************************************************/
143: @Nonnull
144: private PresentationModel internalCreatePresentationModel (@Nonnull final As datum,
145: @Nonnull final Collection<Object> roles)
146: {
147: final var pmFinder = new SCPFinder(this, roles);
148:
149: return contextSnapshot.runWithContexts(new Task<>()
150: {
151: @Override @Nonnull
152: public PresentationModel run()
153: {
154: final var r = RoleFactory.resolveFactories(datum, roles);
155:
156: if (datum.maybeAs(_SimpleComposite_).isPresent())
157: {
158: r.add(SimpleComposite.of(pmFinder));
159: }
160:
161: log.trace(">>>> r for {}: {}", shortId(datum), shortIds(r));
162:
163: return defaultPresentationModelFactory.createPresentationModel(datum, r);
164: }
165: });
166: }
167: }